package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.AuthorType;
import com.hengxinguotong.zhihuichengjian.utils.Constants;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXEditTextView;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.view.SelectListPop;
import com.lidroid.xutils.http.RequestParams;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加公司界面
 * Created by liux on 2017/4/20.
 */

public class AddCompanyActivity extends BaseActivity {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.submit)
    HXTextView submit;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.name_etv)
    HXEditTextView nameEtv;
    @BindView(R.id.companytype_tv)
    HXTextView companytypeTv;
    @BindView(R.id.type_rl)
    RelativeLayout typeRl;
    @BindView(R.id.main_ll)
    LinearLayout mainLl;
    private SelectListPop mSelectListPop;
    private List<AuthorType> typeList;
    private String type = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addcompany);
        ButterKnife.bind(this);
        getList();
    }

    private void addCompany() {
        if (Utils.isEmpty(nameEtv.getEditableText().toString())) {
            showToast("请填写公司名称");
            return;
        }

        if (Utils.isEmpty(type)) {
            showToast("请选择公司类型");
            return;
        }


        RequestParams params = new RequestParams();
        params.addBodyParameter("constructionId", SPUtil.getString(this, "constructionid"));
        params.addBodyParameter("type", type);
        params.addBodyParameter("name", nameEtv.getEditableText().toString());

        Utils.requestData(getResources().getString(R.string.loading), this, "/projectCompany/save/" + SPUtil.getString(this, "uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                showToast("提交成功");
                setResult(1);
                finish();
            }

            @Override
            public void onFailure(String json) {

            }
        });

    }


    @OnClick({R.id.iv_back, R.id.submit, R.id.companytype_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.submit:
                addCompany();
                break;
            case R.id.companytype_tv:
//                showSelectPop(Constants.companyString);
                Intent intent = new Intent(this, MenuActivity.class);
                intent.putExtra("title", "公司类型");
                intent.putExtra("list", (Serializable) typeList);
                startActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        switch (requestCode){
            case 1:
                AuthorType bean = (AuthorType) data.getSerializableExtra("type");
                companytypeTv.setText(bean.getName());
                type = bean.getValue();
                break;
        }
    }

    private void getList(){
        typeList = new ArrayList<>();
        for (int i = 0; i < Constants.companyString.length; i++) {
            AuthorType bean = new AuthorType();
            bean.setName(Constants.companyString[i]);
            bean.setValue(String.valueOf(i+1));
            typeList.add(bean);
        }
    }

    private void showSelectPop(final String[] strings) {
        mSelectListPop = new SelectListPop(this, R.layout.dialog_select_sex, strings, new SelectListPop.OnDirListPopItemClicked() {
            @Override
            public void onPopItemClick(int position, String name) {
                companytypeTv.setText(strings[position]);
//                type = position + 1;
            }
        });
        mSelectListPop.setFocusable(true);
        mSelectListPop.setOutsideTouchable(true);
        mSelectListPop.setBackgroundDrawable(getResources().getDrawable(R.color.color_transparent));
        mSelectListPop.update();
        mSelectListPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        if (mSelectListPop.isShowing()) {
            mSelectListPop.dismiss();
        } else {
            mSelectListPop.showAtLocation(mainLl, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
        }
    }

}
